package top.qrcodepay.plugin;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import top.qrcodepay.plugin.map.MapGenerator;

public class QrcodePay extends JavaPlugin implements Listener {
	
	FileConfiguration config = getConfig();
	
	private int appId;
	
	public static boolean checked = false;
	
	public static String appNumber = "";
	
	@Override
	public void onEnable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>【二维码充值】加载成功！<<<<<<<<<<<");
		getLogger().info(">>> /cz 获取充值二维码 /cz reload 重载插件 <<<");
		getLogger().info(">>>>>产品动态请访问官网 【QrcodePay.top】<<<<<");
		getLogger().info(" ");
		saveDefaultConfig();
		appId = ServiceRequester.checkServerNumber(config.getString("ID"));
		if (appId != 0) {
			checked = true;
			appNumber = config.getString("ID");
			getLogger().info("※当前服务器ID【" + config.getString("ID") + "】验证成功！");
		} else {
			checked = false;
			getLogger().info("※请到QrcodePay.top注册插件并将ID写入config.yml");
		}
		getLogger().info(" ");
		ServiceRequester.heatbeat();
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (command.getName().equalsIgnoreCase("cz")) {
			if (sender.isOp() && args.length > 0 && "reload".equalsIgnoreCase(args[0])) {
				//重载插件
				reloadConfig();
				config = getConfig();
				getLogger().info(" ");
				getLogger().info(">>>>>>>>>>>【二维码充值】重载成功！<<<<<<<<<<<");
				getLogger().info(" ");
				appId = ServiceRequester.checkServerNumber(config.getString("ID"));
				String status;
				if (appId != 0) {
					checked = true;
					appNumber = config.getString("ID");
					status = "插件已注册并通过验证！";
					getLogger().info("※当前服务器ID【" + config.getString("ID") + "】验证成功！");
				} else {
					checked = false;
					status = "请到QrcodePay.top注册插件！";
					getLogger().info("※请到QrcodePay.top注册插件并将ID写入config.yml");
				}
				sender.sendMessage("§6§l[QrcodePay] 重载成功！" + status);
			} else {
				if (sender instanceof Player) {
					if (appId != 0) {
						MapGenerator.generate(appId, sender.getName());
					} else {
						sender.sendMessage("§6§l[QrcodePay] 插件尚未注册，请联系服主注册！");
					}
				} else {
					sender.sendMessage("[QrcodePay] 无法从控制台使用！");
				}
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>【二维码充值】卸载成功！<<<<<<<<<<<");
		getLogger().info(" ");
	}
	
}
